<?php

function __sort_root__($a, $b) {
	if ($a->idx == $b->idx)
	return 0;
	else return ($a->idx > $b->idx ? 1 : -1);
}

class ItemTreeAction extends CAction {
	public function run() {
		$category = "ItemTreeAction [P#".$_GET['project_id']." M#".$_GET['id']."]";
		Yii::log("Loading meeting #".$_GET['id'],CLogger::LEVEL_INFO, $category);
		$this->controller->layout = 'empty';
		$meeting = Meetings::model()->findByPk($_GET['id']);
		if ($meeting == null) {
			throw new CHttpException(404, "Meeting #".$_GET['id']." not found");
		}
		$this->controller->project = $meeting->project;
		$this->controller->pageTitle = $this->controller->project;

		Yii::log("Loading topics of project #".$_GET['id'],CLogger::LEVEL_INFO, $category);
		$tmp = $this->controller->project->topics;
		$topics = array();

		if ($tmp != null){
			Yii::log("Converting #".$_GET['id'],CLogger::LEVEL_INFO, $category);
			while (count($tmp) > 0) {
				$topic = &array_shift($tmp);
				$topic->isNewRecord = false;
				$topics[$topic->key] = $topic;
				$topic->checked = true;
				Yii::log("Found {$topic->key} in database",CLogger::LEVEL_TRACE, "Items");
				if ($topic->children != null) {
					$tmp = array_merge($topic->children, $tmp);
				}
			}
		}

		Yii::log("Update topics tree and find root nodes.",CLogger::LEVEL_INFO, $category);
		$roots = array();
		foreach ($topics as $topic){
			if ($topic->parent_key != null && array_key_exists($topic->parent_key, $topics)) {
				if (!isset($topic->id)) {
					$idx = $topic->idx;
					$topic->parent = $topics[$topic->parent_key];
					$topic->idx = $idx;
				}
			} else {
				$roots[] = $topic;
			}
		}

		$update = false;
		if (isset($_POST['next_button']) || isset($_POST['prev_button'])) {
			//die(CVarDumper::dumpAsString($_POST['ProjectTopic'], 3, true));
			$update = true;
			if (isset($_POST['ProjectTopic'])) {
				Yii::log("Merging post data.",CLogger::LEVEL_INFO, $category);
				foreach ($_POST['ProjectTopic'] as $key => $topic) {
					$arr = $topic;
					$topics[$key]->attributes = $arr;
					unset($arr);
				}
					
				// on enregistre l'arbre
				Yii::log("Saving items.",CLogger::LEVEL_INFO, $category);
					
				if (!$update) $stack = array();
				else $stack = array_merge($roots);
				while (count($stack) > 0) {
					$topic = array_shift($stack);

					ProjectTopicHelper::adaptAllChildrenAndParentStatuses($topic);
					
					Yii::log("Saving item {$topic->key} with id {$topic->id} on project {$topic->project_id}.", CLogger::LEVEL_INFO, $category);
					if ($topic->objective == ProjectTopic::YES) {
						if ($topic->objective_date == null)
							$topic->objective_date = $meeting->meeting_date;
					} else if ($topic->objective == ProjectTopic::INHERITS) {
						$topic->objective_date = $topic->parent->objective_date;
						$topic->objective_needs = null;
						$topic->objective_contacts = null;
					} else {
						$topic->objective_date = null;
						$topic->objective_needs = null;
						$topic->objective_contacts = null;
					}
					
					if ($topic->adviser_validation == ProjectTopic::YES) {
						if ($topic->adviser_validation_date == null)
							$topic->adviser_validation_date = $meeting->meeting_date;
					} else if ($topic->adviser_validation == ProjectTopic::INHERITS) {
						$topic->adviser_validation_date = $topic->parent->adviser_validation_date;
					} else {
						$topic->adviser_validation_date = null;
					}
					
					if ($topic->owner_validation == ProjectTopic::YES) {
						if ($topic->owner_validation_date == null)
							$topic->owner_validation_date = $meeting->meeting_date;
					} else if ($topic->owner_validation == ProjectTopic::INHERITS) {
						$topic->owner_validation_date = $topic->parent->owner_validation_date;
					} else {
						$topic->owner_validation_date = null;
					}
					
					if ($topic->owner_validation != ProjectTopic::YES && $topic->adviser_validation != ProjectTopic::YES){
						$topic->validation_comment = null;
					}
					
					$update = $update & $topic->save();
					foreach ($topic->children as $child) {
						$child->setParent($topic);
						$stack[] = $child;
					}
				}
			}
		}

		usort($roots, "__sort_root__");

		if (!$update) {
			Yii::log("Render.", CLogger::LEVEL_INFO, $category);
			$this->controller->render('items_tree', array (
				'topics' => $topics,
				'roots' => $roots
			));
		} else if (isset($_POST['prev_button'])) {
			$this->controller->redirect(array_merge(array('previousObjectives'), $_GET));
		} else {
			$this->controller->redirect(array_merge(array('endOfMeeting'), $_GET));
		}
	}
}